<?php
class v1_content{
    function index(){
		$data['basic']=$this->yum->config['Manage']['domain'].'?c=content&a=index';
		$params=array();
		if($this->yum->get('order',true)){
			$data['order']=$this->yum->get('order',true);$data['rule']=$this->yum->get('rule',true);
			if(in_array($data['order'],array('sequence','date'))&&in_array($data['rule'],array('desc','asc'))){
				$params['order']['A.'.$data['order']]=$data['rule'];
			}
		}else{
			$data['order']='date';$data['rule']='desc';
			$params['order']['A.'.$data['order']]=$data['rule'];
		}
		if($this->yum->get('search',true)){
			$data['search']=urldecode($this->yum->get('search',true));$data['tab']=$this->yum->get('tab',true);
			if($data['search']!=''){
				if($data['tab']=='mail'){
					$params['where']['B.mail LIKE']='%'.$data['search'].'%';
				}else if($data['tab']=='title'){
					$params['where']['title LIKE']='%'.$data['search'].'%';
				}
				$data['basic'].='&tab='.$data['tab'];
				$data['basic'].='&search='.urlencode($data['search']);
			}
		}
		$sort=$this->yum->model('sort');
	    $data['sort']=$sort->GSort(array('where'=>array('label='=>'content','locked='=>0)));
		$content=$this->yum->model('content');
		$data['total']=$content->GContentFull(array('select'=>'COUNT(*) AS count','where'=>$params['where']));
		$data['total']=empty($data['total'])?0:$data['total'][0]['count'];
		$params['limit']=array(intval($this->yum->get('start')),15);
		$data['content']=$content->GContentFull($params);
		$pagination=$this->yum->library('pagination',array('query_string'=>'start','base_url'=>$data['basic'].'&order='.$data['order'].'&rule='.$data['rule'],'total_rows'=>$data['total'],'per_page'=>15,'first_link'=>'首页','last_link'=>'末页','cur_tag_open'=>'<a href="#" class="active">','cur_tag_close'=>'</a>'));
		$data['pagination']=$pagination->create_links();
		$this->yum->set($data);
		$this->yum->view('index.php');
    }
    function append(){
    	if($this->yum->post('save')){
			$output=array('result'=>0,'msg'=>'');
			$sort=intval($this->yum->post('sort'));
			$title=$this->yum->post('title',true);
			$detail=$this->yum->post('detail',true);
			$sequence=intval($this->yum->post('sequence'));
			if($sort==0){
				$output['msg']='请选择分类目录.';
			}elseif(empty($title)){
				$output['msg']='标题不可为空.';
			}elseif(empty($detail)){
				$output['msg']='内容不可为空.';
			}else{
				$session=$this->yum->library('session');
				$content=$this->yum->model('content');
				$temp=$content->GContent(array('where'=>array('sort='=>$sort,'title='=>$title)));
				if(empty($temp)){
					$affected=$content->IContent(array('sort'=>$sort,'title'=>$title,'detail'=>$detail,'member'=>intval($session->GVAR('member')),'date'=>$this->yum->server('REQUEST_TIME'),'ip'=>$this->yum->server('REMOTE_ADDR'),'sequence'=>$sequence));
					if($affected==1){
						$output['result']=2;
						$output['msg']='添加成功.';
					}else{
						$output['msg']='您输入的数据有误.';
					}
				}else{
					$output['msg']='您输入的标题已存在.';
				}
			}
			header('Content-type:application/json');
			echo json_encode($output);
    	}else{
    		$sort=$this->yum->model('sort');
	    	$data['sort']=$sort->SortTree(array('where'=>array('label='=>'content','locked='=>0)));
	    	$this->yum->set($data);
			$this->yum->view('append.php');
    	}
    }
    function edit(){
    	$content=$this->yum->model('content');
		if($this->yum->post('save')){
			$output=array('result'=>0,'msg'=>'');
			$id=$this->yum->post('id',true);$title=$this->yum->post('title',true);$sort=$this->yum->post('sort',true);
			$detail=$this->yum->post('detail',true);$sequence=intval($this->yum->post('sequence'));
			$affected=$content->UContent(array('where'=>array('id='=>$id),'set'=>array('sort='=>$sort,'title='=>$title,'detail='=>$detail,'sequence='=>$sequence)));
			if($affected==1){
				$output['result']=2;
				$output['msg']='修改成功.';
			}else{
				$output['msg']='请误提交相同或未经修改的数据.';
			}
			header('Content-Type:text/plain');
			echo json_encode($output);
		}else{
			$sort=$this->yum->model('sort');
	    	$data['sort']=$sort->GSort(array('where'=>array('label='=>'content','locked='=>0)));
	    	$data['tree']=$sort->GTree($data['sort']);
			$data['content']=reset($content->GContent(array('where'=>array('id='=>intval($this->yum->get('id'))))));
			$this->yum->set($data);
			$this->yum->view('edit.php');
		}
    }
    function locked(){
		$output=array('result'=>0,'msg'=>'');
		$content=$this->yum->model('content');
		$id=$this->yum->get('id');
		$locked=intval($this->yum->get('locked',true));
		if(is_array($id)){
			$id=array_map('intval',$id);
			$affected=$content->UContent(array('where'=>array('id IN'=>$id),'set'=>array('locked='=>$locked)));
			if($affected==0){
				$output['msg']='重复的数据提交.';
			}else{
				$output['result']=2;
			}
		}else{
			$affected=$content->UContent(array('where'=>array('id='=>intval($id)),'set'=>array('locked='=>$locked)));
			if($affected==1){
				$output['result']=5;
				$output['td']=array('offset'=>6,'data'=>array($locked==0?'正常':'失效'));
				$output['exec']=array('attr'=>array('mixed'=>'sub:{locked:'.($locked==0?10:0).'}'),'text'=>($locked==0?'失效':'激活'));
			}else{
				$output['msg']='状态已变更.';
			}
		}
		header('Content-Type:text/plain');
		echo json_encode($output);
    }
    function wipe(){
		$output=array('result'=>0,'msg'=>'');
		$content=$this->yum->model('content');
		$id=$this->yum->get('id');
		if(is_array($id)){
			$id=array_map('intval',$id);
			$affected=$content->DContent(array('where'=>array('id IN'=>$id)));
			if($affected==0){
				$output['msg']='重复的数据提交.';
			}else{
				$output['result']=2;
			}
		}else{
			$affected=$content->DContent(array('where'=>array('id='=>$id)));
			if($affected==1){$output['result']=1;}else{$output['msg']='已删除.';}
		}
		header('Content-Type:text/plain');
		echo json_encode($output);
    }
}
?>